import { handleActions } from 'redux-actions'
import util from '../util'

const init = {
  data: {},
  commentData: {},
  upData: {},
	list: [],
	feedComment: {},
	feedUp: {},
	amount: 0,
  feed: {}
}

export default handleActions({

  'reset newsFeed': (state) => ({
    ...state,
    ...init
  }),

  'reset newsfeed': (state) => ({
    ...state,
    ...init
  }),

  'fetch feed': (state, { payload }) => ({
    ...state,
    feed: payload
  }),
  /*new*/

  'delete feed': (state, { payload }) => ({
    ...state,
    data: {
      ...state.data,
      feeds: state.data.feeds.filter(feed => feed.feedId != payload)
    }
  }),

  'feed up': (state, { payload }) => {
    const { feedId, n } = payload
    return {
      ...state,
      data: {
        ...state.data,
        feeds: state.data.feeds.map(v => {
          if (v.feedId == feedId) {
            return {
              ...v,
              likeNumber: n == 1? v.likeNumber+1: v.likeNumber - 1,
              isLikable: n == 1? 0: 1
            }
          }
          return v
        })
      }
    }
  },

  'add comment': (state, { payload }) => {
    const { data,pageSize, feedId } = payload
    const { comments=[], amount=0 } = state.commentData[feedId] || {}
    if (comments.length == 0 || comments.length%pageSize != 0) {
      return {
        ...state,
        commentData: {
          ...state.commentData,
          [feedId]: {
            amount: amount+1,
            comments: [
              ...comments,
              data
            ]
          }
        }
      }
    }
    return {
      ...state,
      commentData: {
        ...state.commentData,
        [feedId]: {
          comments,
          amount: amount+1,
        }
      }
    }
  },

  'delete comment': (state, { payload }) => {
    const { feedId, commentId, pageSize } = payload
    const { comments=[], amount=0 } = state.commentData[feedId] || {}
    if (comments.length == amount) {
      return {
        ...state,
        commentData: {
          ...state.commentData,
          [feedId]: {
            amount: amount-1,
            comments: comments.filter(v => v.commentId != commentId)
          }
        }
      }
    }
    return state
  },

  'refresh feedComments': (state, { payload }) => ({
    ...state,
    commentData: {
      ...state.commentData,
      [payload.feedId]: payload.data
    }
  }),

	'add feed': (state, { payload }) => {
		const { feedId } = payload
		return {
			...state,
      data: {
      }
		}
	},

  'fetch feeds': (state, { payload }) => ({
    ...state,
    data: {
      amount: payload.amount,
      feeds: [
        ...(state.data.feeds || []),
        ...payload.feeds
      ]
    }
  }),

  'fetch feedComments': (state, { payload }) => {
    const { feedId, data } = payload
    const { amount, comments } = data
    const currentFeedComment = state.commentData[feedId] || {}
    const { comments: currentComments=[] } = currentFeedComment 
    return {
      ...state,
      commentData: {
        ...state.commentData,
        [payload.feedId]: {
          amount,
          comments: [
            ...currentComments,
            ...comments
          ]
        }
      }
    }
  },

  'fetch feedUps': (state, { payload }) => ({
    ...state,
    upData: {
      ...state.upData,
      [payload.feedId]: payload.data
    }
  }),

  'fetch feed image': (state, { payload }) => ({
    ...state,
    data: {
      ...state.data,
      feeds: state.data.feeds.map(feed => {
        if (feed.feedId == payload.feedId) {
          return {
            ...feed,
            images: feed.images.map((img, i) => {
              if (i == payload.index) {
                return {
                  ...img,
                  large: payload.url
                }
              }
              return img
            })
          }
        }
        return feed 
      })
    } 
  }),

  /**/

	'fetch feedComment list': (state, { payload }) => {
		const { feedId, comments, amount } = payload
		return {
			...state,
			feedComment: {
				...state.feedComment,
				[feedId]: {
					list: comments,
					amount
				}
			}
		}
	},

	'fetch feedUp users': (state, { payload }) => {
		const { list, feedId } = payload
		return {
			...state,
			feedUp: {
				...state.feedUp,
				[feedId]: list
			}
		}
	},

  /*
	'feed up': (state, { payload }) => {
		const { feedId, user } = payload
		const { userId } = user
		return {
			...state,
			feedUp: {
				...state.feedUp,
				[feedId]: [...state.feedUp[feedId], {thumbupUser: user}]
			},
			list: state.list.map(feed => {
				if (feed.feedId === feedId) {
					return {
						...feed,
						isLikable: 0,
						likeNumber: feed.likeNumber+1
					}
				}
				return feed
			}),
      feed: {
        ...state.feed,
        isLikable: 0,
        likeNumber: state.feed.likeNumber+1
      }
		}
	},

	'feed down': (state, { payload }) => {
		const { feedId, user } = payload
		const { userId } = user
		return {
			...state,
			feedUp: {
				...state.feedUp,
				[feedId]: state.feedUp[feedId].filter(obj => obj.thumbupUser.userId != userId)
			},
			list: state.list.map(feed => {
				if (feed.feedId === feedId) {
					return {
						...feed,
						isLikable: 1,
						likeNumber: feed.likeNumber-1
					}
				}
				return feed
			}),
      feed: {
        ...state.feed,
        isLikable: 1,
        likeNumber: state.feed.likeNumber-1
      }
		}
	},
  */

  /*
	'add post': (state, { payload }) => {
		const { feedId } = payload
		return {
			...state,
			list: [payload, ...state.list],
			amount: state.amount+1,
			feedUp: {
				...state.feedUp,
				[feedId]: []
			},
			feedComment: {
				...state.feedComment,
				[feedId]: {}
			}
		}
	},
  */

	'delete post': (state, { payload }) => {
		const feedId = payload
		return {
			...state,
			list: state.list.filter(feed => feed.feedId !== feedId),
			amount: state.amount-1
		}
	},

	'delete post content': (state, { payload }) => {
		const feedId = payload
		return {
			...state,
			feedComment: util.filter(state.feedComment, (v, k) => k !== feedId),
			feedUp: util.filter(state.feedUp, (v, k) => k !== feedId),
		}
	}
}, init)
